<?php
/*前台父级控制器*/
namespace app\common\controller;
use app\index\model\Users;
use think\Controller;

header("Content-type: text/html; charset=utf-8");
class IndexBase extends Controller
{
    const appid='wx884cbb051d6c288d';
    const secret='4eafe388157517864ffddcf676f752e4';
    protected $web;
    protected function _initialize()
    {

        parent::_initialize();
        //session('get_otuijian',input('otuijian','0001'));
        // session('wx',1111);    //测试时使用
        if(!session('?wx')){    ////没有session时，获取微信信息保存session
            $code = input('code'); $state = input('state');
            if($code && $state){
                $getTokenUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this::appid."&secret=".$this::secret."&code=$code&grant_type=authorization_code";
                $accessTokenArr=json_decode(httpGet($getTokenUrl),true);
                if($accessTokenArr['errcode']){echo "<script> alert('".$accessTokenArr['errmsg']."');location.href='/'</script>";exit();}//获取时出现错误。code无效
                $openid=$accessTokenArr['openid'];
                $access_token=$accessTokenArr['access_token'];
                $url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
                $result=httpGet($url);
                $arr1=json_decode($result,true);
                $data['name'] = filterEmoji($arr1['nickname']);
                $data['img_url'] = $arr1['headimgurl'];
                $user = new Users();
                $res = $user->where('openid',$openid)->count();
                if ($res) {
                    model('users')->where('openid',$openid)->update($data);
                }else{
                    $data['openid'] = $openid;
                    $user->save($data);
                    $id = $user->id;
                    //生成专属推荐码
                    $str_tuijian = createNonceStr(2).$id.createNonceStr(1);
                    $qrUrl = "http://hc.huisheng.com?otuijian=".$str_tuijian;
                    $qrcode = get_qr_code($qrUrl);
                    $user->where(['id'=>$id])->update(['tuijian'=>$str_tuijian,'qrcode'=>$qrcode,'otuijian'=>session('get_otuijian')]);

                }
                session('wx',$openid);
				if (session('index_url')) {
                    $this->redirect(session('index_url'));
                }
            }else{
				$sVisitUrl = strtolower(request()->module()).'/'.strtolower(request()->controller()).'/'.strtolower(request()->action());
                session('index_url',$sVisitUrl);
                //当前页完整地址request()->url(true)      urlencode(request()->url(true))
                $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this::appid."&redirect_uri=http://gzh.xinlvhr.com&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                header("Location:".$url);
                exit();
            }
        }
		
        //获取网站配置信息
        $web = db('system')->where(['name'=>'site_config'])->select();
        foreach ($web as $key => $v) {
            $this->web[$v['name']] = unserialize($v['value']);
        }
        $this->assign([
            'web'=>$this->web,
        ]);
    }
}